package com.cskaoyan.mall.commons.util;

import com.cskaoyan.mall.commons.constant.SysRetCodeConstants;
import com.cskaoyan.mall.commons.exception.BizException;
import com.cskaoyan.mall.commons.exception.ProcessException;
import com.cskaoyan.mall.commons.exception.ValidateException;
import com.cskaoyan.mall.commons.result.ResponseData;
import com.cskaoyan.mall.commons.result.ResponseUtil;
import org.springframework.web.bind.annotation.ExceptionHandler;

/**
 * @author MoJunXiang
 * @since 2022/09/21 21:07
 */

public class ExceptionUtil {

    /**
     * @function_name handlerException4biz
     * @idea 将下层抛出的异常转换为 resp返回码
     * @param e
     * @return
     * @author MoJunXiang
     * @since 2022/09/21 21:07
     */
    public static ResponseData handlerException4biz(ResponseData response, Exception e) throws Exception {
        Exception ex = null;

        if (!(e instanceof Exception)) {
            return null;
        }

        if (e instanceof ValidateException) {
            response.setCode(Integer.parseInt(SysRetCodeConstants.REQUEST_DATA_ERROR.getCode()));
            response.setMessage(SysRetCodeConstants.REQUEST_DATA_ERROR.getMessage());
        } else if (e instanceof ProcessException) {
            response.setCode(Integer.parseInt(((ProcessException) e).getErrorCode()));
            response.setMessage(e.getMessage());
        } else if (e instanceof BizException) {
            response.setCode(Integer.parseInt(((BizException) e).getErrorCode()));
            response.setMessage(e.getMessage());
        } else if (e instanceof Exception) {
            // 处理不了，抛给调用方处理
            throw e;
        }

        return response;
    }
}
